const { ipcRenderer } = require("electron");
const Vue = require('vue')
const {defaultConfig, getConfig, applyConfig,setInMemConfig}  = require("../../utils/store")
const {getEntries,isApiUnConfiged}  = require("../../utils/api")
applyConfig()

let biasX = 0
let biasY = 0
const moveS = [0, 0, 0, 0]
function calcS() {
  const res = Math.pow(moveS[0] - moveS[2], 2) + Math.pow(moveS[1] - moveS[3], 2)
  return res < 5
}
function handleMove(e) {
  ipcRenderer.send('ballWindowMove', { x: e.screenX - biasX, y: e.screenY - biasY })
}

const app = Vue.createApp({

  data: () => {
    return {
      isNotMore: true,
      entries: {
        sgv:"请等待",
        direction:""
      },
      opacity: 0.8,
      mainColor: '',
      subColor: '',
      timer:{}
    }
  },

  mounted() {
    const storage = getConfig()
    this.mainColor = storage.mainColor
    this.subColor = storage.subColor
    this.opacity = storage.opacity

    ipcRenderer.on("config", (e, data) => {
      this.opacity = data.opacity
      this.mainColor = data.mainColor
      this.subColor = data.subColor
    })
    // 挂载时获取一次数据
    this.getEntries();

    // 进行任务初始化
    // ipcRenderer.send("updateBall")
    this.timer = setInterval(() => {
      console.log("定时执行");
      this.getEntries();
    }, 1000*30);
  },
  // 清空定时器
  beforeUnmount() {
    // 组件销毁时清除定时器，避免内存泄漏
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  methods: {
    showMore() {
      this.isNotMore = false
      // ipcRenderer.send('setFloatIgnoreMouse', false)
    },
    // 获取数据
    getEntries(){
      // 判断是否完成
      console.log("isApiUnConfiged:",isApiUnConfiged())
      if(isApiUnConfiged()){
        this.entries.sgv = "请配置";
        return;
      }
      //
      let storage = getConfig()
      getEntries().then(res => {
        if(res.length > 0){
          entries =  res[0];
          this.entries.sgv = (entries.sgv/18).toFixed(1)
          this.entries.direction = this.direction2Str(entries.direction)

          //  更新颜色时间
          if(this.entries.sgv >= storage.nsLow && this.entries.sgv <= storage.nsHigh){
            storage.mainColor = defaultConfig.mainColor
            storage.subColor = defaultConfig.subColor
          }else {
            storage.mainColor = defaultConfig.subColor
            storage.subColor = defaultConfig.mainColor
          }

          // 更新最后更新时间
          storage.lastUpdateTime = new Date().getTime()
          // 同步数据
          ipcRenderer.send("updateConfig",storage)

        }
      }).catch(e =>{
        console.log(e);
      })
    },
    // 退出APP
    quitApp(){
      if (calcS())
        ipcRenderer.send("quitApp", "show")
    },
    showConfig() {
      if (calcS())
        ipcRenderer.send("showConfig", "show")
    },
    hideMore() {
      this.isNotMore = true
      // ipcRenderer.send('setFloatIgnoreMouse', true)
    },
    handleMouseDown(e) {
      if (e.button == 2) {
        this.isNotMore = true
        ipcRenderer.send('openMenu')
        return
      }
      biasX = e.x;
      biasY = e.y;
      moveS[0] = e.screenX - biasX
      moveS[1] = e.screenY - biasY
      document.addEventListener('mousemove', handleMove)
    },
    handleMouseUp(e) {
      moveS[2] = e.screenX - e.x
      moveS[3] = e.screenY - e.y
      biasX = 0
      biasY = 0
      document.removeEventListener('mousemove', handleMove)
    },
    // 转换字符串
    direction2Str(direction){
      switch (direction) {
        case "TripleUp":
          return "⤊"
        case "DoubleUp":
          return "⇈"
        case "SingleUp":
          return "↑"
        case "FortyFiveUp":
          return "↗"
        case "Flat":
          return "→"
        case "FortyFiveDown":
          return "↘"
        case "SingleDown":
          return "↓"
        case "DoubleDown":
          return "⇊"
        case "TripleDown":
          return "⤋"
        case "NOT COMPUTABLE":
          return "-"
        case "RATE OUT OF RANGE":
          return "⇕"
        default:
          return "⇼"
      }
    }
  }
})
app.mount("#app")
